import Vue from 'vue'
import Vuex from 'vuex'
import { menu } from '@/utils'
import { getChannels } from '@/api'


Vue.use(Vuex)
export default new Vuex.Store({
  state: {
    menu: [],
    interfaces: [],
    navList: [],
    routerList: [],
    keepLength: 6,
    examineID: '',
    channelList: []
  },
  getters: {
    getMenu(state) {
      return state.menu
    },
    getInterfaces(state) {
      return state.interfaces
    },
    getNavList(state) {
      return state.navList
    },
    getRouterList(state) {
      return state.routerList
    },
    getKeepLength(state) {
      return state.keepLength
    },
    getExamineID(state) {
      return state.examineID
    },
    getChannelList(state) {
      return state.channelList
    }
  },
  mutations: {
    SET_MENU(state, data) {
      state.menu = data
    },
    SET_INTERFACES(state, data) {
      state.interfaces = data
    },
    SET_NAVLIST(state, data) {
      state.navList = data
    },
    SET_ROUTERLIST(state, data) {
      const flag = state.routerList.find(i => i.path === data.path)
      // 默认只添加state.keepLength条数据，超出state.keepLength条，踢掉第一个数据
      if (!flag && !menu.detailList.includes(data.path)) {
        state.routerList.push(data)
      }
      if (state.routerList.length > state.keepLength) {
        state.routerList.shift()
      }
    },
    DELETE_ROUTERLIST(state, index) {
      state.routerList.splice(index, 1)
    },
    SET_EXAMINEID(state, data) {
      state.examineID = data
    },
    SET_CHANNELLIST(state, data) {
      state.channelList = data
    }
  },
  actions: {
    GET_CHANNELLIST({ commit }) {
      return new Promise((resolve, reject) => {
        getChannels().then(res => {
          commit('SET_CHANNELLIST', res)
          resolve()
        }).catch(() => {
          reject()
        })
      })
    }
  }
})
